/*
  RealTimeClock.h - DS1307 Real Time Clock support for Arduino
  Copyright 2010 Wolfgang Klier.  All Rights Reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef REALTIMECLOCK_H_
#define REALTIMECLOCK_H_

extern "C" {
  #include <inttypes.h>
}

// Similar to struct tm on *nix machines.
// Note the differing ranges for some fields.
struct tm {
  uint8_t tm_sec;   // seconds [0,60]
  uint8_t tm_min;   // minutes [0,59]
  uint8_t tm_hour;  // hour [0,23]
  uint8_t tm_mday;  // day of month [1,31]
  uint8_t tm_mon;   // month of year [1,12]
  uint8_t tm_year;  // year [0,99]
  uint8_t tm_wday;  // day of week [1,7] (Sunday = 7)
};

// RealTimeClock provides an easy interface to the DS1307 real time clock chip.
// Note: RealTimeClock depends on the Write library.
class RealTimeClock {
 public:
	RealTimeClock();

  // Initialize I2C subsystem with default I2C address
  void begin();
  // Initialize I2C subsytem with the provided custom I2C address
  void begin(int i2c_address);

  // Get the current date and time from the DS1307 chip.
  // getDate expects the clock to have been set in 24h format.
  struct tm getDate();

  // Set the time in the DS1307 to the passed time.
  // Note that setDate does *not* do any error checking. If the ranges of values
  // in the passed date are invalid the result is undefined.
  void setDate(struct tm date);

 private:
	int i2c_address_;
};

extern RealTimeClock RTC;

#endif /* REALTIMECLOCK_H_ */
